package com.zlkj.pro.modular.business.service.impl;

import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.system.api.pojo.user.SysUserDTO;
import cn.stylefeng.roses.kernel.system.api.pojo.user.request.SysUserRequest;
import cn.stylefeng.roses.kernel.system.modular.user.service.SysUserService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlkj.pro.core.consts.ProjectConstants;
import com.zlkj.pro.core.util.ConvertUtil;
import com.zlkj.pro.modular.business.entity.CustomerContactsEntity;
import com.zlkj.pro.modular.business.mapper.CustomerContactsMapper;
import com.zlkj.pro.modular.business.model.dto.CustomerContactsDTO;
import com.zlkj.pro.modular.business.model.vo.CustomerContactsVO;
import com.zlkj.pro.modular.business.service.CustomerContactsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author liyang
 * @date 2024/4/6
 * @time 14:34
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class CustomerContactsServiceImpl extends ServiceImpl<CustomerContactsMapper, CustomerContactsEntity> implements CustomerContactsService {

    @Autowired
    private CustomerContactsMapper customerContactsMapper;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public PageResult<CustomerContactsVO> findPage(CustomerContactsDTO dto) {
        Integer pageNo = dto.getPageNo();
        Integer pageSize = dto.getPageSize();
        dto.setUserId(LoginContext.me().getLoginUser().getUserId());
        extracted(dto);
        List<CustomerContactsVO> list = customerContactsMapper.findPage(dto);
        Long total = customerContactsMapper.findPageCount(dto);
        return PageResultFactory.createPageResult(list, total, pageSize, pageNo);
    }

    private void extracted(CustomerContactsDTO dto) {
        SysUserRequest sysUserRequest = new SysUserRequest();
        sysUserRequest.setUserId(dto.getUserId());
        SysUserDTO currentUser = sysUserService.detail(sysUserRequest);
        if (CollectionUtils.isEmpty(currentUser.getGrantRoleIdList())) {
            return;
        }
        Long roleId = currentUser.getGrantRoleIdList().get(0);
        if (ProjectConstants.ROLE_ID_BMSH_MANAGER.equals(roleId)) {
            dto.setRoleId(1);
        } else if (ProjectConstants.ROLE_ID_BMSH_YG.equals(roleId)) {
            dto.setRoleId(2);
        } else if (ProjectConstants.ROLE_ID_AGENT_MANAGER.equals(roleId)) {
            dto.setRoleId(3);
        } else if (ProjectConstants.ROLE_ID_JL.equals(roleId)) {
            dto.setRoleId(4);
        } else if (ProjectConstants.ROLE_ID_YG.equals(roleId)) {
            dto.setRoleId(5);
        }
    }

    @Override
    public boolean edit(CustomerContactsDTO dto) {
        CustomerContactsEntity entity = ConvertUtil.turn(dto, CustomerContactsEntity.class);
        return super.updateById(entity);
    }

}
